local xiaojing = fk.CreateSkill{
  name = "hanqing__xiaojing",
}

Fk:loadTranslationTable{
  ["hanqing__xiaojing"] = "枭獍",
  [":hanqing__xiaojing"] = "每回合限一次，你可以将中央区一张牌置于牌堆顶，视为使用一张基本牌。若为<font color='red'>♥</font>，"..
  "你移动场上一张牌，否则下次此法置顶与使用的牌其中之一须为【杀】、另一张须为【闪】。",

  ["#hanqing__xiaojing"] = "枭獍：将中央区一张牌置于牌堆顶，视为使用一张基本牌",
  ["#hanqing__xiaojing-move"] = "枭獍：请移动场上一张牌",

  ["$hanqing__xiaojing1"] = "我心饱之愈发，养则益凶，岂食一公子可餍足？",
  ["$hanqing__xiaojing2"] = "无饵不得鱼，汝欲钩浮龙豹，当以身作饵。",
}

xiaojing:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic",
  prompt = "#hanqing__xiaojing",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    if player:getMark(xiaojing.name) > 0 then
      all_names = {"slash", "jink"}
    end
    local names = player:getViewAsCardNames(xiaojing.name, all_names)
    if #names == 0 then return end
    return UI.CardNameBox { choices = names, all_choices = all_names }
  end,
  expand_pile = function (self)
    return Fk:currentRoom():getBanner("@$CenterArea")
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected == 0 and table.contains(Fk:currentRoom():getBanner("@$CenterArea"), to_select) then
      local card = Fk:getCardById(to_select)
      if player:getMark(xiaojing.name) > 0 then
        if self.interaction.data == "slash" then
          return card.trueName == "jink"
        else
          return card.trueName == "slash"
        end
      else
        return true
      end
    end
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data or #cards ~= 1 then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = xiaojing.name
    card:addFakeSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local cards = use.card.fake_subcards
    if Fk:getCardById(cards[1]).suit == Card.Heart then
      room:setPlayerMark(player, xiaojing.name, 0)
      use.extra_data = {}
      use.extra_data.hanqing__xiaojing = true
    else
      room:setPlayerMark(player, xiaojing.name, 1)
    end
    room:moveCards({
      ids = cards,
      toArea = Card.DrawPile,
      moveReason = fk.ReasonPut,
      skillName = xiaojing.name,
      proposer = player,
      moveVisible = true,
    })
  end,
  after_use = function (self, player, use)
    local room = player.room
    if use.extra_data and use.extra_data.hanqing__xiaojing then
      if player.dead or #room:canMoveCardInBoard() < 2 then return end
      local targets = room:askToChooseToMoveCardInBoard(player, {
        skill_name = xiaojing.name,
        prompt = "#hanqing__xiaojing-move",
        cancelable = false,
      })
      room:askToMoveCardInBoard(player, {
        target_one = targets[1],
        target_two = targets[2],
        skill_name = xiaojing.name,
      })
    end
  end,
  enabled_at_play = function(self, player)
    if player:usedSkillTimes(xiaojing.name, Player.HistoryTurn) == 0 then
      if player:getMark(xiaojing.name) == 0 then
        return true
      else
        return table.find(Fk:currentRoom():getBanner("@$CenterArea"), function (id)
          return Fk:getCardById(id).trueName == "jink"
        end)
      end
    end
  end,
  enabled_at_response = function(self, player, response)
    if response or player:usedSkillTimes(xiaojing.name, Player.HistoryTurn) > 0 then return end
    local names = player:getMark(xiaojing.name) > 0 and {"slash", "jink"} or Fk:getAllCardNames("b")
    if #player:getViewAsCardNames(xiaojing.name, names) > 0 then
      if player:getMark(xiaojing.name) > 0 then
        if #player:getViewAsCardNames(xiaojing.name, {"slash"}) > 0 and
          table.find(Fk:currentRoom():getBanner("@$CenterArea"), function (id)
            return Fk:getCardById(id).trueName == "jink"
          end) then
          return true
        end
        if #player:getViewAsCardNames(xiaojing.name, {"jink"}) > 0 and
          table.find(Fk:currentRoom():getBanner("@$CenterArea"), function (id)
            return Fk:getCardById(id).trueName == "slash"
          end) then
          return true
        end
      else
        return table.find(Fk:currentRoom():getBanner("@$CenterArea"), function (id)
          return Fk:getCardById(id).type == Card.TypeBasic
        end)
      end
    end
  end,
})

xiaojing:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

xiaojing:addAI(nil, "vs_skill")

return xiaojing
